package io.huaguoguo;

import cn.hutool.core.io.IoUtil;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import static cn.hutool.core.util.CharsetUtil.CHARSET_GBK;

/**
 * 描述：  <br>
 * CreateDate: 2021/11/24 <br>
 *
 * @author huaguoguo
 */
public class BIOServer {

    public static void main(String[] args) throws IOException {
        //创建ServerSocket
        ServerSocket serverSocket = new ServerSocket(7777);

        System.out.println("服务器启动了");

        while (true) {

            System.out.println("线程信息 id =" + Thread.currentThread().getId() + " 名字=" + Thread.currentThread().getName());
            //监听，等待客户端连接
            System.out.println("等待连接....");
            final Socket socket = serverSocket.accept();
            System.out.println("连接到一个客户端");
            //可以和客户端通讯
            handler(socket);
        }
    }

    private static void handler(Socket socket) throws IOException {
        String read = IoUtil.read(socket.getInputStream(), CHARSET_GBK);
        System.out.println("读取到的数据：\n" + read);
        socket.close();
    }
}
